package graph;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Map.Entry;

public class Vertex<ItemType, CostType> {

	private ItemType									item;
	private Map<Vertex<ItemType, CostType>, CostType>	neighbours	= new LinkedHashMap<Vertex<ItemType, CostType>, CostType>();

	public Vertex(ItemType object) {
		this.item = object;
	}

	public ItemType getItem() {
		return item;
	}

	public void addSide(Vertex<ItemType, CostType> to, CostType cost) {
		neighbours.put(to, cost);
	}

	public Set<Vertex<ItemType, CostType>> getNeighbours() {
		return neighbours.keySet();
	}

	public ArrayList<Vertex<ItemType, CostType>> countBreadthFirst(ArrayList<Vertex<ItemType, CostType>> vertexList, ArrayList<Vertex<ItemType, CostType>> checkedList) {
		checkedList.add(this);
		Set<Map.Entry<Vertex<ItemType, CostType>, CostType>> set = neighbours.entrySet();
		Iterator<Entry<Vertex<ItemType, CostType>, CostType>> i = set.iterator();
		vertexList.add(this);
		Set<Vertex<ItemType, CostType>> wachtRij = new LinkedHashSet<Vertex<ItemType, CostType>>();
		while (i.hasNext()) {
			Map.Entry<Vertex<ItemType, CostType>, CostType> me = i.next();
			if (!checkedList.contains(me.getKey())) {
				vertexList.add(me.getKey());
				checkedList.add(me.getKey());
				wachtRij.addAll(me.getKey().getNeighbours());
			}
		}

		Iterator<Vertex<ItemType, CostType>> j = wachtRij.iterator();
		while (j.hasNext()) {
			Vertex<ItemType, CostType> vertex = j.next();
			if (!checkedList.contains(vertex)) {
				vertexList = vertex.countBreadthFirst(vertexList, checkedList);
			}
		}
		return vertexList;
	}

	public ArrayList<Vertex<ItemType, CostType>> countDepthFirst(ArrayList<Vertex<ItemType, CostType>> vertexList, ArrayList<Vertex<ItemType, CostType>> checkedList) {
		checkedList.add(this);
		Set<Map.Entry<Vertex<ItemType, CostType>, CostType>> set = neighbours.entrySet();
		Iterator<Entry<Vertex<ItemType, CostType>, CostType>> i = set.iterator();
		vertexList.add(this);
		while (i.hasNext()) {
			Map.Entry<Vertex<ItemType, CostType>, CostType> me = i.next();
			if (!checkedList.contains(me.getKey()))
				vertexList = (me.getKey().countDepthFirst(vertexList, checkedList));
		}
		return vertexList;
	}

	
	public CostType getCost(ItemType to) {
		Set<Map.Entry<Vertex<ItemType, CostType>, CostType>> set = neighbours.entrySet();
		Iterator<Entry<Vertex<ItemType, CostType>, CostType>> i = set.iterator();
		while (i.hasNext()) {
			Map.Entry<Vertex<ItemType, CostType>, CostType> me = i.next();
			if (me.getKey().getItem().equals(to)) {
				return me.getValue();
			}
		}
		return null;
	}

	public String toString() {
		String ret = "";
		ret += item;
		Set<Map.Entry<Vertex<ItemType, CostType>, CostType>> set = neighbours.entrySet();

		if (set.isEmpty())
			return ret;
		Iterator<Entry<Vertex<ItemType, CostType>, CostType>> i = set.iterator();
		ret += "[";
		while (i.hasNext()) {
			Map.Entry<Vertex<ItemType, CostType>, CostType> me = i.next();
			ret += "[" + me.getValue().toString() + ">" + me.getKey().getItem().toString() + "]";
		}
		ret += "]";
		return ret;
	}

}
